package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceInfo;
import com.java.entity.event.RaceInfoEvent;
import com.java.mapper.RaceAccountMapper;
import com.java.mapper.RaceInfoMapper;
import com.java.service.RaceInfomationService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RaceInfomationServiceImpl implements RaceInfomationService {

	@Autowired
	private RaceInfoMapper infoMapper;
	@Autowired
	private RaceAccountMapper accountMapper;
	
	@Override
	public BaseRespone createInfo(RaceInfo info) {
		info.setInfoId(MyTools.getUUID()).setInfoTime(new Date());
		int row = infoMapper.insertSelective(info);
		return BaseRespone.result(row, "已创建");
	}

	@Override
	public BaseRespone upodateInfo(RaceInfo info) {
		int row = infoMapper.updateSelective(info);
		return BaseRespone.result(row, "已修改");
	}

	@Override
	public BaseRespone delInfo(String infoId) {
		int row = infoMapper.deleteByKey(infoId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public TableRespone getInfos(String publishId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(null != publishId) {
			params.put("infoPublishId", publishId);
		}
		List<RaceInfoEvent> list = infoMapper.selectAll(params, page, size);
		for (RaceInfoEvent raceInfoEvent : list) {
			raceInfoEvent.setInfoPublish(accountMapper.selectByKey(raceInfoEvent.getInfoPublishId()));
		}
		long count = infoMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}

}
